function Promise(executor){
    this.status = 'pending'
    this.value = null
    this.reason = null

    this.onFulfilledArr = []
    this.onRejectedArr = []
    const resolve = value => {
        if(value instanceof Promise){
            return value.then(resolve, reject)
        }
        setTimeout(() => {
            if(this.status === 'pending'){
                this.value = value
                this.status = 'fulfilled'
                this.onFulfilledArr.forEach(func => {
                    func(value)
                })
            }
        })
    }
    const reject = reason => {
        setTimeout(() => {
            if(this.status === 'pending'){
                this.reason = reason
                this.status = 'rejected'
                this.onRejectedArr.forEach(func => {
                    func(reason)
                })
            }
        })
    }
    try {
        executor(resolve, reject)
    } catch(e){
        reject(e)
    }
}


const resolvePromise = (promise2, result, resolve, reject) => {
    //......................
}


Promise.prototype.then = function(onfulfilled, onrejected){
    onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data                                                               
    onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
    let promise2
    if(this.status === 'fulfilled'){
        return promise2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    let result = onfulfilled(this.value)
                    resolvePromise(promise2, result, resolve, reject)
                } catch(e) {
                    reject(e)
                }
            })
        })
    }
    if(this.status === 'rejected'){
        return promise2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    let result = onrejected(this.reason)
                } catch(e) {
                    reject(e)
                }
            })
        })
    }
    if(this.status === 'pending'){
        return promise2 = new Promise((resolve, reject) => {
            this.onFulfilledArr.push(() => {
                try {
                    let result = onfulfilled(this.value)
                    resolvePromise(promise2, result, resolve, reject)
                } catch(e) {
                    reject(e)
                }
            })
            this.onRejectedArr.push(() => {
                try {
                    let result = onrejected(this.reason)
                    resolvePromise(promise2, result, resolve, reject)
                } catch(e) {
                    reject(e)
                }
            })
        })
    }
}